﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace MH3GAPP.Common
{
    class ErrorLogHelper
    {
        #region variable
        private static string logPath = @"./ErrorLog/";
        private static string fileName = @"ErrorLog.log";
        private string filePath = logPath + fileName;
        private object locker = new object();
        private string funcName = "Unknown Method";
        const bool methodFlag = true;
        #endregion

        #region Constructor
        public ErrorLogHelper()
        {
            lock (locker)
            {
                if (!System.IO.Directory.Exists(logPath))
                {
                    System.IO.Directory.CreateDirectory(logPath);
                }

                if (!System.IO.File.Exists(filePath))
                {
                    FileStream fs = System.IO.File.Create(filePath);
                    fs.Close();
                }
            }

            StackTrace st = new StackTrace(true);
            string method_name = st.GetFrame(1).GetMethod().Name.ToString();
            string name_space = st.GetFrame(1).GetMethod().DeclaringType.FullName.ToString();
            funcName = name_space + "::" + method_name;
        }
        #endregion

        #region Output Log
        /// <summary>
        /// 记录日志包括方法名
        /// </summary>
        /// <param name="message">日志信息</param>
        public void Log(string message)
        {
            try
            {
                lock (locker)
                {
                    System.IO.StreamWriter objWStream = new System.IO.StreamWriter(filePath, true, System.Text.Encoding.Default);

                    objWStream.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + funcName + ":" + message);

                    objWStream.Close();

                    objWStream = null;
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("LogError:" + ex.Message);
            }
        }
        #endregion

        #region Normal Log
        /// <summary>
        /// 一般的错误记录，不记录方法名
        /// </summary>
        /// <param name="message">记录信息</param>
        public void NormalLog(string message)
        {
            try
            {
                lock (locker)
                {
                    System.IO.StreamWriter objWStream = new System.IO.StreamWriter(filePath, true, System.Text.Encoding.Default);

                    objWStream.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + message);

                    objWStream.Close();

                    objWStream = null;
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("LogError:" + ex.Message);
            }
        }
        #endregion
    }
}
